/**
 * @file app_mqtt.c
 * @author Ai-Thinker Seahi
 * @brief
 * @version 0.1
 * @date 2021-09-02
 *
 * @copyright Copyright (c) 2021
 *
 */
#include "esp_err.h"
#include "esp_log.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "driver/gpio.h"
#include "app_mqtt.h"
#include "cJSON.h"
#include "RGB_device.h"
static char* TAG = "APP MQTT";

bool mqtt_connect = 0;
int RGB_STATUS = 0;
esp_mqtt_client_handle_t my_client;
/**
 * @brief MQTT json 数据解析
 *
 * @param mqtt_data
 */
static void mqtt_data_handle_to_RGB(char* mqtt_data)
{
    cJSON* root = cJSON_Parse(mqtt_data);
    cJSON* params = NULL;
    cJSON* RGB = NULL;
    if (root == NULL) {
        ESP_LOGE(TAG, "Is not json data");
        cJSON_Delete(root);
        return;
    }
    params = cJSON_GetObjectItem(root, "params");
    if (params == NULL) {
        //ESP_LOGE(TAG, "No params data");
        cJSON_Delete(root);
        return;
    }
    RGB = cJSON_GetObjectItem(params, "RGB");
    rgb_set_color(RGB->valueint);
    cJSON_Delete(root);
}
/**
 * @brief MQTT 事件处理函数
 *
 * @param handler_args
 * @param base
 * @param event_id
 * @param evnet_data
 */
static void mqtt_event_handler(void* handler_args, esp_event_base_t base, int32_t event_id, void* evnet_data)
{

    esp_mqtt_event_handle_t evnet = (esp_mqtt_event_handle_t)evnet_data;
    switch (evnet->event_id) {
        case MQTT_EVENT_CONNECTED:
            ESP_LOGI(TAG, "MQTT CONNECET");
            mqtt_connect = true;
            esp_mqtt_client_subscribe(evnet->client, My_SubTopic, 1);

            break;
        case MQTT_EVENT_DISCONNECTED:
            ESP_LOGI(TAG, "MQTT DISCONNECTED");
            mqtt_connect = false;
            break;
        case MQTT_EVENT_SUBSCRIBED:
            ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", evnet->msg_id);

            break;
        case MQTT_EVENT_UNSUBSCRIBED:
            ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", evnet->msg_id);

            break;
        case MQTT_EVENT_PUBLISHED:
            ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", evnet->msg_id);

            break;
        case MQTT_EVENT_DATA:
            ESP_LOGI(TAG, "MQTT_EVENT_DATA");
            printf("TOPIC=%.*s\r\n", evnet->topic_len, evnet->topic);
            printf("DATA=%.*s\r\n", evnet->data_len, evnet->data);
            mqtt_data_handle_to_RGB(evnet->data);
            break;
        case MQTT_EVENT_ERROR:
            ESP_LOGE(TAG, "MQTT ERROR");

            break;
        default:
            ESP_LOGI(TAG, "Other event id:%d", evnet->event_id);
            break;
    }
    memset(evnet, 0, sizeof(esp_mqtt_event_t));
}

/**
 * @brief MQTT启动并连接
 *
 */
void app_mqtt_start(void)
{
    //配置连接信息
    esp_mqtt_client_config_t my_client_cfg = {
        .host = My_Host,
        .client_id = My_ClientID,
        .username = My_UserName,
        .password = My_Password,
        .keepalive = 120,
        .disable_auto_reconnect = false,
    };
    //初始化MQTT客户端
    my_client = esp_mqtt_client_init(&my_client_cfg);
    esp_mqtt_client_register_event(my_client, ESP_EVENT_ANY_ID, mqtt_event_handler, my_client);
    esp_mqtt_client_start(my_client);
}
